# This file is part of OrangeSpider. OrangeSpider is a meta model based
# development tool.
# 
# OrangeSpider is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# OrangeSpider is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with OrangeSpider.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2009 Stefan Nikolaus <OrangeSpiderMaster@googlemail.com>

import Ui
import Component
import Tree
import FastEdit
import Project
import Graph
import Package
import Persistence
from MetaModel import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class Manager(Component.Component):
    def __init__(self):
        Component.Component.__init__(self)
        self.metaModels = {}
        self.components = {}
        self.componentTypes = {}
        
        r = buildMetaModel("Component", "SpiderCOMP", Watcher())
        self.repository = r
        self.repository.watcher.addListener(Ui.Emitter(), "Emitter")
        self._initComponentModel()
        self.registerComponentType(Graph.Graph)
        self.registerComponentType(FastEdit.FastEdit)
        self.registerComponentType(Ui.MatrixBrowser)
        self.registerComponentType(Project.ProjectComponent)
        self.registerComponentType(Component.DirectoryImporter)
        self.registerComponentType(Component.IniFileReader)
        self.registerComponentType(Component.MetaModelConverter)
        self.registerComponentType(Package.PackageManager)
        
    def interface(self, interfaceName):
        return None

    def registerComponentType(self, componentClass):
        r = self.repository
        if "setupComponentType" in componentClass.__dict__:
            ct = componentClass.setupComponentType(r)
            typename = ct["Name"]
            self.componentTypes[typename] = componentClass
        else:
            print "unable to register component " + componentClass.__name__
            
    def createComponent(self, typename):
        if typename in self.componentTypes:
            newComponent = self.componentTypes[typename]()
        else:
           print "Unkown component", typename
           newComponent = None

        if newComponent:
           iManagedComponent = newComponent.interface("Spider.IManagedComponent")
           if iManagedComponent:
               r = self.repository
               ct = r.find("ComponentType", "Name", typename)
               assert(ct)
               comp = r.Class("Component")
               uuid = r.uuid()
               comp["UUID"] = uuid
               r.Assoc(ct, comp, "Instance")
               #iManagedComponent.setupInfo(comp)
               self.components[uuid] = newComponent
        return newComponent

    def createMetaModel(self, name, type, path):
        r = buildMetaModel(name, type, Watcher())
        emitter = r.watcher.addListener(Ui.Emitter(), "Emitter")
        r["FileName"] = path
        uuid = r["UUID"]
        self.metaModels[uuid] = r
        return r
    def openMetaModel(self, path, uuid):
        if uuid in self.metaModels:
            return self.metaModels[uuid]
        else:
            im = Persistence.Importer()
            r = im.load(path)
            emitter = r.watcher.addListener(Ui.Emitter(), "Emitter")
            r["FileName"] = path
            self.metaModels[uuid] = r
            if r["Type"] == "SpiderMMM":
                r.watcher.addListener(Builder.ConstraintManagerMMM(), 
                    "ConstraintManagerMMM")
            return r

    def addMetaModel(self, path, r):
        emitter = r.watcher.addListener(Ui.Emitter(), "Emitter")
        r["FileName"] = path
        uuid = r["UUID"]
        print uuid
        self.metaModels[uuid] = r
    def removeMetaModel(self, uuid):
        if uuid in self.metaModels:
            del self.metaModels[uuid]

    def getMetaMetaModel(self, typ):
        for r in self.metaModels.itervalues():
            if r["Type"] == "SpiderMMM":
                if r["Name"] == typ:
                    return r
        return None

    def getMetaMetaModels(self):
        found = []
        for r in self.metaModels.itervalues():
            if r["Type"] == "SpiderMMM":
                found.append(r)
        return found
    def isBuiltIn(self, type):
        return type in ["SpiderMMM", "SpiderTST", "SpiderINI", "SpiderSQL", "SpiderCOMP", 
                        "SpiderDIAGRAM", "SpiderFILE", "SpiderPROJECT"]

    def _initComponentModel(self):
        r = self.repository
        ifc = r.Class("Interface")
        ifc["Name"] = "Spider.IProject"
        ifc = r.Class("Interface")
        ifc["Name"] = "Spider.IManagedComponent"

